const express = require('express');
const multer = require('multer');
const app = express();
const port = 3000;




app.all('*', function(req, res, next) {  
    res.header("Access-Control-Allow-Origin", "*"); // 允许所有来源访问
    res.header("Access-Control-Allow-Headers", "X-Requested-With,Content-Type"); // 允许的请求头
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"); // 允许的请求方法
    res.header("X-Powered-By",' 3.2.1') // 自定义响应头
    res.header("Content-Type", "application/json;charset=utf-8"); // 响应类型

    // 在此处可以添加其他响应头或处理逻辑

    next(); // 调用 next() 以继续到下一个中间件或路由处理程序  
});

// 配置multer存储
const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, 'upload/');
  },
  filename: (req, file, cb) => {
    cb(null, file.fieldname + '-' + Date.now() + '.' + file.originalname.split('.').pop());
  }
});

// 初始化multer
const upload = multer({ storage: storage });

// 上传文件的路由
app.post('/upload', upload.single('file'), (req, res) => {
  res.send('文件上传成功');
});

// 启动服务器
app.listen(port, () => {
  console.log(`服务器运行在 http://localhost:${port}`);
});
